import pygame
import time

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH = 640
HEIGHT = 480
TILE_SIZE = 32
PLAYER_SPEED = 3
BOMB_TIMER = 3  # 炸弹爆炸时间（秒）
EXPLOSION_DURATION = 1  # 爆炸持续时间（秒）

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("炸弹人游戏")

# 加载图片
player_img = pygame.Surface((TILE_SIZE, TILE_SIZE))
player_img.fill(WHITE)
bomb_img = pygame.Surface((TILE_SIZE, TILE_SIZE))
bomb_img.fill(RED)
explosion_img = pygame.Surface((TILE_SIZE, TILE_SIZE))
explosion_img.fill(RED)

# 地图定义
map_data = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]

# 玩家类
class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def move(self, dx, dy):
        new_x = self.x + dx * PLAYER_SPEED
        new_y = self.y + dy * PLAYER_SPEED
        new_col = new_x // TILE_SIZE
        new_row = new_y // TILE_SIZE
        if map_data[new_row][new_col] == 0:
            self.x = new_x
            self.y = new_y

    def draw(self):
        screen.blit(player_img, (self.x, self.y))

# 炸弹类
class Bomb:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.placed_time = time.time()

    def is_exploded(self):
        return time.time() - self.placed_time >= BOMB_TIMER

    def draw(self):
        screen.blit(bomb_img, (self.x, self.y))

# 爆炸类
class Explosion:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.exploded_time = time.time()

    def has_expired(self):
        return time.time() - self.exploded_time >= EXPLOSION_DURATION

    def draw(self):
        screen.blit(explosion_img, (self.x, self.y))

# 创建玩家
player = Player(TILE_SIZE, TILE_SIZE)
bombs = []
explosions = []

# 游戏主循环
clock = pygame.time.Clock()
running = True
while running:
    screen.fill(BLACK)

    # 绘制地图
    for row in range(len(map_data)):
        for col in range(len(map_data[row])):
            if map_data[row][col] == 1:
                pygame.draw.rect(screen, WHITE, (col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE))

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player.move(0, -1)
            elif event.key == pygame.K_DOWN:
                player.move(0, 1)
            elif event.key == pygame.K_LEFT:
                player.move(-1, 0)
            elif event.key == pygame.K_RIGHT:
                player.move(1, 0)
            elif event.key == pygame.K_SPACE:
                bomb_x = (player.x // TILE_SIZE) * TILE_SIZE
                bomb_y = (player.y // TILE_SIZE) * TILE_SIZE
                bombs.append(Bomb(bomb_x, bomb_y))

    # 更新炸弹状态
    new_bombs = []
    for bomb in bombs:
        if bomb.is_exploded():
            explosions.append(Explosion(bomb.x, bomb.y))
        else:
            new_bombs.append(bomb)
    bombs = new_bombs

    # 更新爆炸状态
    new_explosions = []
    for explosion in explosions:
        if not explosion.has_expired():
            new_explosions.append(explosion)
    explosions = new_explosions

    # 绘制炸弹和爆炸
    for bomb in bombs:
        bomb.draw()
    for explosion in explosions:
        explosion.draw()

    # 绘制玩家
    player.draw()

    # 更新显示
    pygame.display.flip()
    clock.tick(60)

# 退出游戏
pygame.quit()